//
//  CustomCell.m
//  19--Homework 汽车之家
//
//  Created by 冀永金 on 16/8/9.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "CustomCell.h"
#import "HotBrandCollectionViewCell.h"
#import "HotBrandModel.h"
@interface CustomCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *hotMarr;
@property (nonatomic, strong) NSDictionary *dicOfHot;
@end
@implementation CustomCell
//http://app.api.autohome.com.cn/autov4.6/cars/brands-a2-pm2-v4.6.1-ts635580073583652962.html
//http://cars.app.autohome.com.cn/cars_v5.8.0/cars/getmarks-a2-pm1.json
//选车神奇
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self config];
        [self handleData];
    }
    return self;
}
- (void)handleData {
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSMutableURLRequest *requestHot = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://223.99.255.20/cars.app.autohome.com.cn/dealer_v6.0.0/dealer/hotbrands-pm2.json"]];
    requestHot.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:requestHot completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.dicOfHot = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *listArray = _dicOfHot[@"result"][@"list"];
            self.hotMarr = [NSMutableArray array];
            for (NSDictionary *dic in listArray) {
                HotBrandModel *hot = [HotBrandModel hotBrandWithDic:dic];
                [self.hotMarr addObject:hot];
            }
            
            [self.collectionView reloadData];
        });
    }];
    [dataTask resume];

    
}
- (void)config {
    _layout = [[UICollectionViewFlowLayout alloc]init];
    _layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
       _collectionView = [[UICollectionView alloc]initWithFrame:self.contentView.bounds collectionViewLayout:_layout];
    [self.contentView addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[HotBrandCollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
}
- (void)layoutSubviews {
    [super layoutSubviews];
     _collectionView.frame = self.bounds;
    _layout.itemSize = CGSizeMake((self.bounds.size.width - 60) / 5, (self.bounds.size.height - 25) / 2);

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.hotMarr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotBrandCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    HotBrandModel *hot = self.hotMarr[indexPath.row];
    [cell setHotModel:hot];
    
    return cell;
}
@end
